/**
 * 
 */
package org.softTank.BpelTranslator.Impl;

import java.util.ArrayList;
import java.util.List;

import org.softTank.BcpModel.BcpActivity;
import org.softTank.BcpModel.BcpAssign;
import org.softTank.BcpModel.BcpControl;
import org.softTank.BcpModel.BcpReceive;
import org.softTank.BpelModel.TActivity;
import org.softTank.BpelModel.TAssign;
import org.softTank.BpelModel.TSequence;
import org.softTank.BpelTranslator.ActivityParser;
import org.softTank.BpelTranslator.AssignParser;
import org.softTank.BpelTranslator.ProcessParser;

/**
 * @author Yuzone
 * 
 */
public class SimpleProcessParserImpl implements ProcessParser {

	private AssignParser assignParser;
	private ActivityParser activityParser;
	private List<BcpAssign> assignList;

	public SimpleProcessParserImpl() {
	}

	public SimpleProcessParserImpl(AssignParser value, ActivityParser value2) {
		assignParser = value;
		activityParser = value2;
	}

	public ActivityParser getActivityParser() {
		return activityParser;
	}

	public void setActivityParser(ActivityParser activityParser) {
		this.activityParser = activityParser;
	}

	public AssignParser getAssignParser() {
		return assignParser;
	}

	public void setAssignParser(AssignParser assignParser) {
		this.assignParser = assignParser;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.softTank.BpelTranslator.ProcessParser#parseProcess(org.softTank.BcpModel
	 * .BcpReceive)
	 */
	@Override
	public TActivity parseProcess(BcpReceive value) {
		TAssign rootAssign = new TAssign();
		rootAssign.setName("assign");
		TSequence sequence = new TSequence();
		sequence.getActivity().add(rootAssign);
		assignList = new ArrayList<BcpAssign>();

		BcpActivity iterator = value;
		sequence.getActivity().add(activityParser.parseActivity(iterator));
		while (iterator.getInferiors().size() != 0) {
			iterator = iterator.getInferiors().get(0);
			if (iterator instanceof BcpControl) {
				sequence.getActivity().add(
						assignParser.createAssign(((BcpControl) iterator)
								.getAssignList()));
				rootAssign.getCopyOrExtensionAssignOperation().addAll(
						assignParser
								.createStructDefinition(((BcpControl) iterator)
										.getAssignList()));
				assignList.addAll(((BcpControl) iterator).getAssignList());
				continue;
			}
			sequence.getActivity().add(activityParser.parseActivity(iterator));
		}

		return sequence;
	}

	@Override
	public List<BcpAssign> extractStruct(BcpReceive value) {
		if (assignList == null) {
			parseProcess(value);
		}

		return assignList;
	}

}
